<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>

	<head>
		<title>业务处理-采购入库-编辑采购入库</title>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
		<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
	</head>

	<body>
		<div id="mainContent">
			<div id="mainContent_content">
				<div class="page_header">
					<a href="<%=basePath%>pickingReturnContrlooer/selectByWhere.do">采购入库</a>
					<i class="icon-double-right"></i>
					<a>编辑采购入库</a>
				</div>

				<div class="btn rightBtn">
					<input type="button" value="新增物品" onclick="toAddPurchaseRequestItem();" class="btn btn1"></input>
					<input type="button" value="修改" onclick="saveAddPurchaseRequest();" class="btn btn2"></input>
					<input type="button" value="返回" onclick="location.href='<%=basePath%>warehouseOrder/selectByWhere.do'" class="btn btn1"></input>
					<input type="button" value="刷新" onclick="location.reload();" class="btn btn2"></input>
				</div>
				<form id="generalStorageForm" class="form">
					<input type="hidden" value="${wo1.purchaseordereid }" name="purchaseordereid"></input>
					<input type="hidden" value="${userID  }" name="lastpersonid"></input>
					<div id="requisitionForm" class="box">
						<p>
							<label> 入库单号</label>
							<input type="text" readonly class="form-control" value="${wo1.warehousenumber}"></input>

							<label> 入库人</label>
							<input type="text" readonly class="form-control" value="${buyerPerson.loginName }"></input>

							<label> 类型</label>
							<select name="materialMaterial2" class="form-control select" id="materialMaterial2" onchange="gradeChange()">
								<option value="2" <c:if test="${wo1.materialMaterial=='2'}">selected</c:if>>IMPA物料</option>
								<option value="1" <c:if test="${wo1.materialMaterial=='1'}">selected</c:if>>普通物料</option>
							</select>

							<label> 状态</label>
							<select name="status" class="form-control select">
								<option value="0" <c:if test="${wo1.status=='0'}">selected</c:if>>未提交</option>
								<option value="1" <c:if test="${wo1.status=='1'}">selected</c:if>>已提交</option>
							</select>

						</p>
						<p>
							<label> 仓库</label>
							<select name="warehouseid" onchange="gradeChange()" class="form-control select">
								<c:forEach items="${warehouseList }" var="warehouse">
									<option value="${warehouse.warehousemanagementid }" <c:if test="${wo1.warehouseid==warehouse.warehousemanagementid}">selected</c:if>>${warehouse.warehousename }</option>
								</c:forEach>
							</select>

							<label> 说明</label>
							<input type="text" class="form-control" name="company" value="${wo1.company}"></input>

							<label> 备注</label>
							<input type="text" class="form-control doubleWidth" name="remarks" value="${wo1.remarks}"></input>

							<input type="hidden" name="address" value="${wo1.address}">
							<input type="hidden" name="oldaddress" value="${wo1.address}">
						</p>
					</div>
					<div class="tableBox box">
						<table class="table table-bordered table_text_center">
							<thead>
								<tr>
									<th>采购订单单号</th>
									<th>代码</th>
									<th>物品名称</th>
									<th>物品条码</th>
									<th>品牌</th>
									<th>库存</th>
									<th>过期时间</th>
									<th>采购数量</th>
									<th>规格</th>
									<th>数量</th>
									<th>操作</th>
								</tr>
							</thead>
							<tbody id="purchaseRequestItemTbody">
								<c:forEach items="${list2 }" var="item" varStatus="i">
									<tr>
										<td>${item.purchaseOrder3.orderNumber }</td>
										<td>${item.IntemInformation.itemcode }</td>
										<td>${item.IntemInformation.chinesename }</td>
										<td>${item.IntemInformation.articlebarcode }</td>
										<td>${item.IntemInformation.brand }</td>
										<td>${item.IntemInformation.currentstock }</td>

										<td><input type='text' name='expiryDate' readonly onclick='WdatePicker({skin:"whyGreen",dateFmt:"yyyy-MM-dd"})' value="<fmt:formatDate value='${item.WarehouseOrder_iteminformation.expiryDate }' type='date' />" class="form-control" />

										</td>

										<td><input name='itemNumber2' maxlength='8' oninput='calculations(this)' type='text' value="${item.WarehouseOrder_iteminformation.number}" class="form-control" /></td>
										<td>
											<select oninput='calculations2(this)' class="form-control select">
												<option value='${item.IntemInformation.salesspecifications }'>${item.IntemInformation.salesspecifications }${item.IntemInformation.salesunit }</option>
												<option value='${item.IntemInformation.purchasespecifications }'>${item.IntemInformation.purchasingunit }/${item.IntemInformation.purchasespecifications } ${item.IntemInformation.salesunit }</option>
											</select>
										</td>

										<td>
											<input type="hidden" name="generalStorageItemInformationId" value="${item.WarehouseOrder_iteminformation.id }" />
											<input type='text' name='itemNumber' readonly onkeyup='limitInputNumber(this);' onchange='limitInputNumber(this);' value="${item.WarehouseOrder_iteminformation.number}" class="form-control" />
										</td>
										<td>
											<a onclick='deleteItem(this);' class='btn btn-xs btn_danger'>删除</a> <input type="hidden" name='itemIds' value='${item.IntemInformation.inteminformationid}' />
											<input type='hidden' name='purchaseorderIteminformationId' value='${item.WarehouseOrder_iteminformation.sourceID}' /></td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
					<div class="box updateBox ">
						<p>
							<label>创建人</label>
							<input type="text" class="form-control doubleWidth" readonly value="${createPerson.loginName } "></input>
							<label>创建时间</label>
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${wo1.createtime  }' pattern=" yyyy-MM-dd HH:mm:ss " /> "></input>
							<br>
							<label>最后处理人</label>
							<input type="text" class="form-control doubleWidth" readonly value="${lastperson.loginName } "></input>
							<label>最后处理时间</label>
							<input type="text" class="form-control doubleWidth" readonly value="<fmt:formatDate value='${wo1.finalprocessingtime  }' pattern=" yyyy-MM-dd HH:mm:ss " /> "></input>
						</p>
					</div>
				</form>
			</div>
			<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
			<jsp:include page="/jsp/storage/purchaseWarehous/addPurchaseRequestItem.jsp"></jsp:include>
		</div>
		<script src="<%=basePath%>js/jquery-3.2.1.min.js"></script>
		<script src="<%=basePath%>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
		<script src="<%=basePath%>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
		<script src="<%=basePath%>My97DatePickers/WdatePicker.js"></script>
		<script src="<%=basePath%>js/public.js"></script>
		<script type="text/javascript">
			function gradeChange() {
				$("#add_generalStorage #materialMaterial").val($("#materialMaterial2 option:selected").val());
				$('#purchaseRequestItemTbody').empty();
			}

			$(function() {
				// 任何需要执行的js特效 
				$("#add_generalStorage #materialMaterial").val($("#materialMaterial2 option:selected").val());
			});
			/* 输入采购数量自动计算数量 */
			function calculations(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).val().replace(/\D/g, '')) * ($(obj).parent().next().find("option:selected").val()));
			}
			/* 选择规格下拉框在自动计算数量 */
			function calculations2(obj) {
				$(obj).parent().parent().find("input[name='itemNumber']").val(($(obj).parent().parent().find("input[name='itemNumber2']").val().replace(/\D/g, '')) * ($(obj).find("option:selected").val()));
			}

			function addToHasAddTable(item) {
				$.ajax({
					url: "<%=basePath%>warehouseOrder/listAddItem2.do",
					type: "post",
					async: false,
					dataType: "json",
					data: "purchaseOrderID=" + item.purchaseOrderID + "&oldaddress=" + $("#generalStorageForm [name='oldaddress']").val() + "&purchaseordereid=" + $("#generalStorageForm [name='purchaseordereid']").val(),
					success: function(data) {
						$("#generalStorageForm [name='address']").val(item.purchaseOrderID);
						var html = "";
						$.each(data, function(i, n) {

							html += "<tr>";
							html += "<td>" + data[i].purchaseOrder1.orderNumber + "</td>";
							html += "<td>" + data[i].IntemInformation.itemcode + "</td>";
							html += "<td>" + data[i].IntemInformation.chinesename + "</td>";
							html += "<td>" + data[i].IntemInformation.articlebarcode + "</td>";
							html += "<td>" + data[i].IntemInformation.brand + "</td>";
							html += "<td>" + data[i].IntemInformation.currentstock + "</td>";
							html += "<td><input type='text' name='expiryDate' readonly onclick='WdatePicker({skin:\"whyGreen\",dateFmt:\"yyyy-MM-dd\"})' class='form-control'/></td>";
							html += "<td><input name='itemNumber2' maxlength='8'  oninput='calculations(this)' type='text' value='" + data[i].purchaseorder_iteminformation.number + "' class='form-control'/></td>";
							html += "<td><select oninput='calculations2(this)' class='form-control select'>" +
								"<option value='" + data[i].IntemInformation.salesspecifications + "'>" + data[i].IntemInformation.salesspecifications + "" + data[i].IntemInformation.salesunit + "</option>" +
								"<option value='" + data[i].IntemInformation.purchasespecifications + "'>" + data[i].IntemInformation.purchasespecifications + "" + data[i].IntemInformation.salesunit + "/" + data[i].IntemInformation.purchasingunit + "</option>" +
								"</select></td>";
							html += "<td><input type='text' name='itemNumber' readonly value='" + data[i].purchaseorder_iteminformation.number + "' class='form-control'/></td>";
							/* html+="<td><input type='text' name='itemNumber' onkeyup='limitInputNumber(this);' onchange='limitInputNumber(this);'/></td>"; */
							html += "<td><a onclick='deleteItem(this);' class='btn btn-xs btn_danger'>删除</a>";
							html += "<input type='hidden' name='purchaseorderIteminformationId' value='" + data[i].purchaseorder_iteminformation.id + "' />";
							html += "<input type='hidden' name='itemIds' value='" + data[i].IntemInformation.inteminformationid + "' /></td>";
							html += "</tr>";
						});

						$("#purchaseRequestItemTbody").append(html);
					},
					error: function() {
						Prompt.alert("提示", "根据订单查询物品失败！");
					}
				});

			}

			function deleteItem(obj) {
				/* var dd=$(obj).parent().parent().find("[name='generalStorageItemInformationId']").val();
				alert(dd); */
				$(obj).parent().parent().remove();
			}

			function validateForm() {
				var length = $("#purchaseRequestItemTbody [name='itemIds']").length;
				if(!length > 0) {
					Prompt.alert("提示", "物品种类数量必须大于0！")
					return false;
				}
				var arr = $("#purchaseRequestItemTbody [name='itemNumber']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '' || num <= 0) {
						Prompt.alert("提示", "物品采购数量必须大于0！")
						return false;
					}
				}
				/* 检查日期是否填写 */
				var arr = $("#purchaseRequestItemTbody [name='expiryDate']");
				for(var i = 0; i < arr.length; i++) {
					var num = $(arr[i]).val();
					if(num.trim() == '') {
						Prompt.alert("提示", "必须填写日期！")
						return false;
					}
				}
				return true;
			}

			function saveAddPurchaseRequest() {
				if(!validateForm()) return;
				$.ajax({
					url: "<%=basePath%>warehouseOrder/update.do",
					type: "post",
					async: false,
					dataType: "json",
					data: $("#generalStorageForm").serialize(),
					success: function(data) {
						if(data.result == "success") {
							Prompt.alert("提示", "采购申请保存成功！", function() {
								location.href = "<%=basePath%>warehouseOrder/selectByWhere.do";
							});
						} else {
							Prompt.alert("提示", "采购申请保存失败！");
						}
					},
					error: function() {
						Prompt.alert("提示", "采购申请保存失败！");
					}
				});
			}
		</script>
	</body>

</html>